<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      
      <el-form-item>
        <el-button type="primary" @click="handleRefresh">
          <Icon icon="ep:refresh-right" class="mr-5px" /> 刷新客户池
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="客户名称" align="center" prop="customerName" width="150" />
      <el-table-column label="联系人" align="center" prop="contactName" width="100" />
      <el-table-column label="联系电话" align="center" prop="phone" width="120" />
      <el-table-column label="电子邮箱" align="center" prop="email" width="150" />
      <el-table-column label="客户等级" align="center" prop="levelId">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_CUSTOMER_LEAVE" :value="scope.row.levelId" />
        </template>
      </el-table-column>
      <el-table-column label="客户来源" align="center" prop="sourceId" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_CUSTOMER_SOURCE" :value="scope.row.sourceId" />
        </template>
      </el-table-column>
      <el-table-column label="所属行业" align="center" prop="industryId" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_INDUSTRY" :value="scope.row.industryId" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="最后修改时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleDetail(scope.row.id)"
            v-hasPermi="['oa:customer:query']"
          >
            查看
          </el-button>
          <el-button
            link
            type="success"
            @click="handleReceive(scope.row)"
            v-hasPermi="['oa:customer:update']"
          >
            领取
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>

  <!-- 详情弹窗 -->
  <CustomerDetail ref="detailRef" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { CustomerApi, CustomerVO } from '@/api/oa/customer'
import CustomerDetail from './CustomerDetail.vue'
import { useUserStore } from '@/store/modules/user'
import { useRouter } from 'vue-router'

/** 抢客宝页面 */
defineOptions({ name: 'CustomerGrab' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const userStore = useUserStore() // 用户store
const router = useRouter() // 路由实例

const loading = ref(true) // 列表的加载中
const list = ref<CustomerVO[]>([]) // 列表的数据
const queryParams = reactive({
  customerName: undefined,
  sourceId: undefined,
  industryId: undefined,
})
const queryFormRef = ref() // 搜索的表单
const detailRef = ref() // 详情 Ref

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CustomerApi.refreshCustomerPool()
    list.value = data
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 刷新客户池 */
const handleRefresh = async () => {
  try {
    await CustomerApi.refreshCustomerPool()
    message.success('刷新成功')
    await getList()
  } catch {}
}

/** 查看详情操作 */
const handleDetail = (id: number) => {
  detailRef.value?.open(id)
}

/** 处理领取按钮点击 */
const handleReceive = async (row: CustomerVO) => {
  try {
    await CustomerApi.assignCustomer({
      id: row.id,
      recipientId: userStore.getUser.id,
      recipientName: userStore.getUser.nickname
    })
    message.success('领取成功')
    // 跳转到客户跟进页面
    router.push({
      path: '/oa/customer/follow',
      query: { id: row.id }
    })
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script> 